package com.exam.paper.service;

import com.alibaba.fastjson.JSON;
import com.exam.common.entity.Cascader;
import com.exam.common.entity.PageRequest;
import com.exam.common.until.IdWorker;
import com.exam.paper.mapper.SelectMapper;
import com.exam.paper.pojo.Category;
import com.exam.paper.mapper.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : CategoryServiceImpl
 * @Description : 分类实现类
 * @Author : WHB
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SelectMapper selectMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 分页查询试题分类信息
     * @param page
     * @param size
     * @return
     */
    public PageRequest<Category> queryCategoryByPage(Integer page, Integer size){
        //启动分页
        PageHelper.startPage(page,size);
        //获取列表
        List<Category> list = categoryMapper.selectAll();
        //解析分页结果
        PageInfo<Category> info = new PageInfo<>(list);
        return new PageRequest<>(info.getTotal(),list);
    }

    /**
     * 获取父分类列表
     * @return
     */
    public List<Category> queryPCategory() {
        //获取父分类
        List<Category> list = queryCategoryListByPid((long) 0);

        //设置默认分类(默认为父分类 ParentId=0)
        Category category = new Category();
        category.setId((long) 0);
        category.setName("无");
        list.add(0,category);
        return list;
    }

    /**
     * 新增分类
     * @param data
     * @return
     */
    public void saveCategory(String data) {
        //实例化
        Category category = JSON.parseObject(data,Category.class);
        category.setId(idWorker.nextId());

        //通过parentId获取parentName
        Long parentId = category.getParentId();
        //如果为父分类，父分类名称为无
        if (parentId == 0){
            category.setParentName("无");
            category.setIsParent(1);
        }else {
            category.setParentName(queryCategoryById(parentId).getName());
            category.setIsParent(0);
        }
        category.setCreatTime(new Date());

        categoryMapper.insert(category);
    }

    /**
     * 修改分类
     * @param data
     * @return
     */
    public void editCategory(String data) {
        //实例化
        Category category = JSON.parseObject(data,Category.class);

        //设置修改时间
        category.setReviseTime(new Date());

        //由于前端问题，ParentName里其实是ParentId
        //Long parentId = Long.valueOf(category.getParentName());

        try {
            Long PID  = category.getParentId();
            //如果为父分类，父分类名称为无
            if (category.getParentId() == 0){
                category.setParentName("无");
                category.setIsParent(1);
            }else {
                category.setParentName(queryCategoryById(category.getParentId()).getName());
                category.setIsParent(0);
            }
            categoryMapper.updateByPrimaryKeySelective(category);
        }catch (Exception e){
            categoryMapper.updateByPrimaryKeySelective(category);
        }

    }

    /**
     * 通过parentId获取Category列表
     * @param Pid
     * @return
     */
    public List<Category> queryCategoryListByPid(Long Pid) {
        Category category = new Category();
        category.setParentId(Pid);
        return categoryMapper.select(category);
    }

    /**
     * 通过Id获取Category
     * @param id
     * @return
     */
    public Category queryCategoryById(Long id) {
        Category category = new Category();
        category.setId(id);
        return categoryMapper.selectOne(category);
    }

    /**
     * 删除分类
     * @param id
     */
    public void deleteCategroy(Long id) {
        Category category = new Category();
        category.setId(id);
        categoryMapper.delete(category);
    }

    public List<Cascader> queryCascader() {
        Category Category = new Category();
        Category.setIsParent(1);
        //父分类列表
        List<Category> pCategoryList = categoryMapper.select(Category);
        //cascader列表
        List<Cascader> cascaderList = new ArrayList<>();
        for (int i = 0; i <pCategoryList.size() ; i++) {
            //取出父分类
            Category pcategory = pCategoryList.get(i);
            //构造父列表（缺子分类）
            Cascader cascader = new Cascader(pcategory.getId(),pcategory.getName());
            //构建查询子孩子 parentid相同的
            Category category = new Category();
            category.setParentId(pcategory.getId());
            //得到所有子分类
            List<Category> child = categoryMapper.select(category);
            //子分类构建成Cascader列表
            List<Cascader> chiledList = new ArrayList<>();
            for (int j = 0; j < child.size(); j++) {
                Category c = child.get(j);
                Cascader cascader1 = new Cascader(c.getId(),c.getName());
                chiledList.add(j,cascader1);
            }
            //加入子分类
            cascader.setChildren(chiledList);
            //构造最终cascader
            cascaderList.add(i,cascader);
        }


        return cascaderList;
    }
}
